<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="结算月份" prop="statementMonth">
        <el-date-picker clearable v-model="queryParams.statementMonth" type="month" value-format="yyyy-MM"
                        placeholder="选择结算月份"/>
      </el-form-item>
      <el-form-item label="结算状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择商户类型" filterable clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.statement_status)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择商户类型" filterable clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.app_tenant_type)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户名称" prop="tenantId">
        <el-select v-model="queryParams.tenantId" @change="chooseTenant" filterable clearable placeholder="请输入商户名称"
                   @keyup.enter.native="handleQuery"
        >
          <el-option v-for="tenant in tenants" :key="parseInt(tenant.id)" :label="tenant.name"
                     :value="parseInt(tenant.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分公司名称" prop="subTenantId">
        <el-select v-model="queryParams.subTenantId" @change="chooseSubTenant" filterable clearable
                   placeholder="请输入分公司名称"
                   @keyup.enter.native="handleQuery"
        >
          <el-option v-for="subTenant in subTenants" :key="parseInt(subTenant.id)" :label="subTenant.name"
                     :value="parseInt(subTenant.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="结算时间" prop="statementTime">
        <el-date-picker v-model="queryParams.statementTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['pekingfish:app-final-statement:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === -1" size="mini" type="text" icon="el-icon-edit"
                     @click="handleFinalStatement(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:app-final-statement:update']"
          >结算
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="自增主键" v-if="false" align="center" prop="id"/>
      <el-table-column label="结算月份" align="center" prop="statementMonth"/>
      <el-table-column label="结算状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.statement_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="商户类型" align="center" prop="type">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_account_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="分公司名称" align="center" prop="subTenantName"/>
      <el-table-column label="商户名称" align="center" prop="tenantName"/>
      <el-table-column label="商户分润总额" align="center" prop="tenantPrices"/>
      <el-table-column label="分公司分润总额" align="center" prop="subTenantPrices"/>
      <el-table-column label="扣款金额" align="center" prop="refundMoneys"/>
      <el-table-column label="商户积分总额" align="center" prop="tenantIntegralPrices"/>
      <el-table-column label="分公司积分总额" align="center" prop="subTenantIntegralPrices"/>
      <el-table-column label="商户结算总金额" align="center" prop="tenantStatementPrices"/>
      <el-table-column label="分公司结算总金额" align="center" prop="subTenantStatementPrices"/>
      <el-table-column label="结算时间" align="center" prop="statementTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.statementTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

  </div>
</template>

<script>
import {
  createAppFinalStatement,
  updateAppFinalStatement,
  deleteAppFinalStatement,
  getAppFinalStatement,
  getAppFinalStatementPage,
  exportAppFinalStatementExcel, updateStatus
} from "@/api/pekingfish/appFinalStatement";
import {getBranchCompanyByTenantId} from "@/api/merchantManagement/branchCompany";
import {getStoreBySuTenantId} from "@/api/merchantManagement/store";
import {confirmReceipt} from "@/api/pekingfish/appMerchantOrder";
import {getBusinessList} from "@/api/merchantManagement/business";

export default {
  name: "AppFinalStatement",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算单列表
      list: [],
      // 商户名称
      tenants: [],
      // 分公司名称
      subTenants: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        statementMonth: null,
        status: null,
        type: null,
        subTenantId: null,
        tenantPrices: null,
        statementTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    // 商户名称
    getBusinessList().then(response => {
      this.tenants = response.data.list
    })
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getAppFinalStatementPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    chooseTenant(tenantId) {
      console.log(tenantId + '获取的id')
      // 分公司名称
      getBranchCompanyByTenantId(tenantId).then(response => {
        this.subTenants = response.data
      })
    },

    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        statementMonth: undefined,
        status: undefined,
        type: undefined,
        subTenantId: undefined,
        tenantPrices: undefined,
        subTenantPrices: undefined,
        tenantIntegralPrices: undefined,
        subTenantIntegralPrices: undefined,
        tenantStatementPrices: undefined,
        subTenantStatementPrices: undefined,
        id: undefined,
      };
      this.resetForm("form");
    },

    handleFinalStatement(row) {
      const id = row.id
      this.$modal.confirm('是否确认同意结算编号为"' + id + '"的数据结算?').then(function () {
        return updateStatus(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('结算成功')
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加结算单";
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有结算单数据项?').then(() => {
        this.exportLoading = true;
        return exportAppFinalStatementExcel(params);
      }).then(response => {
        this.$download.excel(response, '结算单.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
